#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
struct TreeNode {
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
};

int preordersize(struct TreeNode* root)
{
    if (root == NULL)
        return 0;
    return preordersize(root->left) + preordersize(root->right) + 1;
}

void _preorderTraversal(struct TreeNode* root, int* arr, int* n)
{
    if (root == NULL)
        return;
    arr[*n] = root->val;
    ++(*n);
    _preorderTraversal(root->left, arr, n);
    _preorderTraversal(root->right, arr, n);
}

int* preorderTraversal(struct TreeNode* root, int* returnSize) {
    *returnSize = preordersize(root);
    int* array = malloc(sizeof(int) * (*returnSize));
    int n = 0;
    _preorderTraversal(root, array, &n);
    return array;
}